package com.hh.game.service.gateway.manager.service.config;

import cn.hutool.core.io.FileUtil;
import com.hh.game.service.gateway.manager.service.ServiceConfigManager;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Configuration
@ConfigurationProperties(prefix = "app.service")
public class ServiceConfiguration implements Cloneable {
    private String path; ///本地加载路径
    private String serverAddr; //配置中心地址
    private String group = "DEFAULT_GROUP"; //nacos group
    private Set<String> dataIdSet; // service路由配置dataId

    private ServiceConfigManager manager;

    private String configID; //本配置[app.service] 配置的dataId
    private String configGroup = "DEFAULT_GROUP";//本配置[app.service] 配置的group


    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public Set<String> getDataIdSet() {
        return dataIdSet;
    }

    public void setDataId(Set<String> dataId) {
        this.dataIdSet = dataId;
    }
    public void setDataId(String dataId) {
        Set<String> set = new HashSet<>();
        set.add(dataId);
        setDataId(set);
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getServerAddr() {
        return serverAddr;
    }

    public void setServerAddr(String serverAddr) {
        this.serverAddr = serverAddr;
    }

    public String getConfigID() {
        return configID;
    }

    public void setConfigID(String configID) {
        String name = FileUtil.mainName(configID);
        if(name.endsWith("-${spring.profiles.active}")){
            this.configID = name.substring(0 ,name.length() - "-${spring.profiles.active}".length())
                    + "." + FileUtil.extName(configID);
        }else {
            this.configID = configID;
        }
    }

    public String getConfigGroup() {
        return configGroup;
    }

    public void setConfigGroup(String configGroup) {
        this.configGroup = configGroup;
    }

    @Override
    public ServiceConfiguration clone() throws CloneNotSupportedException {
        ServiceConfiguration config = new ServiceConfiguration();
        config.path = this.path;
        config.serverAddr = this.serverAddr;
        config.group = this.group;
        config.dataIdSet = dataIdSet;
        return config;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ServiceConfiguration that = (ServiceConfiguration) o;
        return Objects.equals(path, that.path) &&
                Objects.equals(serverAddr, that.serverAddr) &&
                Objects.equals(group, that.group) &&
                Objects.equals(dataIdSet, that.dataIdSet) ;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}
